<template>
  <div class="shipped-orders">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>已发货订单管理</span>
          <div class="right">
            <el-input
              v-model="searchQuery"
              placeholder="输入订单号搜索"
              class="search-input"
              clearable
              @clear="handleSearch"
              @keyup.enter="handleSearch"
            >
              <template #prefix>
                <el-icon><Search /></el-icon>
              </template>
            </el-input>
            <el-button type="primary" @click="handleSearch">搜索</el-button>
          </div>
        </div>
      </template>

      <el-table
        v-loading="loading"
        :data="orderList"
        border
        style="width: 100%"
      >
        <el-table-column prop="orderNo" label="订单号" width="180" />
        <el-table-column prop="createTime" label="下单时间" width="180" />
        <el-table-column prop="customerName" label="客户名称" width="120" />
        <el-table-column prop="totalAmount" label="订单金额" width="120">
          <template #default="scope">
            ¥{{ scope.row.totalAmount }}
          </template>
        </el-table-column>
        <el-table-column prop="shippingInfo" label="物流信息" width="200" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default>
            <el-tag type="success">已发货</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="200">
          <template #default="scope">
            <el-button
              type="primary"
              link
              @click="handleViewDetails(scope.row)"
            >
              查看详情
            </el-button>
<!--            <el-button-->
<!--              type="success"-->
<!--              link-->
<!--              @click="handleConfirmReceipt(scope.row)"-->
<!--            >-->
<!--              确认收货-->
<!--            </el-button>-->
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 订单详情对话框 -->
    <el-dialog
      v-model="detailsVisible"
      title="订单详情"
      width="60%"
    >
      <OrderDetails
        v-if="detailsVisible"
        :order-id="currentOrderId"
      />
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import OrderDetails from '@/components/OrderDetails.vue'

// 状态变量
const loading = ref(false)
const orderList = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const searchQuery = ref('')
const detailsVisible = ref(false)
const currentOrderId = ref('')

// 获取订单列表
const getOrderList = async () => {
  loading.value = true
  try {
    const res = await fetch('/api/orders/shipped', {
      params: {
        page: currentPage.value,
        pageSize: pageSize.value,
        query: searchQuery.value
      }
    })
    const data = await res.json()
    orderList.value = data.list
    total.value = data.total
  } catch (error) {
    ElMessage.error('获取订单列表失败')
  } finally {
    loading.value = false
  }
}

// 查看订单详情
const handleViewDetails = (row) => {
  currentOrderId.value = row.id
  detailsVisible.value = true
}

// 确认收货
// const handleConfirmReceipt = async (row) => {
//   try {
//     await fetch(`/api/orders/${row.id}/confirm-receipt`, {
//       method: 'POST'
//     })
//     ElMessage.success('确认收货成功')
//     getOrderList()
//   } catch (error) {
//     ElMessage.error('确认收货失败')
//   }
// }

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  getOrderList()
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getOrderList()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  getOrderList()
}

onMounted(() => {
  getOrderList()
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.right {
  display: flex;
  gap: 10px;
}

.search-input {
  width: 200px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
